package com.suixun.controller;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.authority.controller.BaseManager;
import com.suixun.entity.attendancemachine.ExtAttendanceMachine;
import com.suixun.entity.page.PageModel;
import com.suixun.service.AttendanceMachineService;
import com.suixun.util.Constant;
import com.suixun.util.DateUtil;

@Controller
@RequestMapping(value = "/attendanceMachine")
public class AttendanceMachineController extends BaseManager {
	@Resource
	private AttendanceMachineService attendanceMachineService;

	/**
	 * <p>
	 * Title: getAttendanceMachinePage
	 * </p>
	 * <p>
	 * 功能描述: 分页查询考勤机
	 * </p>
	 * 
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @param pageNumber
	 * @param pageCount
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getAttendanceMachinePage.json")
	public Object getAttendanceMachinePage(String schoolName, String factoryCode, String deviceCode, Integer pageNumber,
			Integer pageCount) {
		try {
			return success(attendanceMachineService.getAttendanceMachinePage(schoolName, factoryCode, deviceCode,
					pageNumber, pageCount));
		} catch (Exception e) {
			logger("AttendanceMachineController getAttendanceMachinePage:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * <p>
	 * Title: getLogAttendanceMachinePage
	 * </p>
	 * <p>
	 * 功能描述: 分页查询考勤机记录
	 * </p>
	 * 
	 * @param attendanceMachineId
	 * @param sortStatus
	 * @param sortParam
	 * @param pageNumber
	 * @param pageCount
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getLogAttendanceMachinePage.json")
	public Object getLogAttendanceMachinePage(Integer attendanceMachineId, Integer sortStatus, Integer sortParam,
			Integer pageNumber, Integer pageCount) {
		try {
			if(sortStatus == null) {
				sortStatus = 0;
			}
			if(sortParam == null) {
				sortParam = 0;
			}
			return success(attendanceMachineService.getLogAttendanceMachinePage(attendanceMachineId, sortStatus,
					sortParam, pageNumber, pageCount));
		} catch (Exception e) {
			logger("AttendanceMachineController getLogAttendanceMachinePage:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * <p>
	 * Title: addAttendanceMachine
	 * </p>
	 * <p>
	 * 功能描述: 新增考勤机
	 * </p>
	 * 
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @param name
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "addAttendanceMachine.json")
	public Object addAttendanceMachine(String schoolName, String factoryCode, String deviceCode, String name) {
		try {
			String result = attendanceMachineService.addAttendanceMachine(schoolName, factoryCode, deviceCode, name);
			if (result == null) {
				return success();
			} else {
				return fail(result);
			}
		} catch (Exception e) {
			logger("AttendanceMachineController addAttendanceMachine:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * <p>
	 * Title: updateAttendanceMachine
	 * </p>
	 * <p>
	 * 功能描述: 修改考勤机
	 * </p>
	 * 
	 * @param attendanceMachineId
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @param name
	 * @param status
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateAttendanceMachine.json")
	public Object updateAttendanceMachine(Integer attendanceMachineId, String schoolName, String factoryCode,
			String deviceCode, String name, Boolean status) {
		try {
			String result = attendanceMachineService.updateAttendanceMachine(attendanceMachineId, schoolName,
					factoryCode, deviceCode, name, status);
			if (result == null) {
				return success();
			} else {
				return fail(result);
			}
		} catch (Exception e) {
			logger("AttendanceMachineController addAttendanceMachine:" + e.getMessage(), e);
			return fail(Constant.EXCEPTION.OPERATE_FAIL);
		}
	}

	/**
	 * <p>Title: reportAttendanceMachine</p>
	 * <p> 功能描述: 导出考勤机列表</p>
	 * @param schoolName
	 * @param factoryCode
	 * @param deviceCode
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "reportAttendanceMachine.json")
	public Object reportAttendanceMachine(String schoolName, String factoryCode, String deviceCode) {
		try {
			boolean bln = checkRight();
			if (!bln) {
				toRedirect(404);
			}

			ArrayList<String> listParm = new ArrayList<String>();
			listParm.add("名称");
			listParm.add("编号");
			listParm.add("生产厂商");
			listParm.add("投放学校");
			listParm.add("投放状态");
			listParm.add("创建时间");
			listParm.add("当前状态");

			List<List<String>> listValue = new LinkedList<List<String>>();
			PageModel<ExtAttendanceMachine> page = attendanceMachineService.getAttendanceMachinePage(schoolName,
					factoryCode, deviceCode, 1, Integer.parseInt(Constant.Excel.EXPORT_ROWS));
			List<ExtAttendanceMachine> list = page.getQueryresult();
			List<String> value = null;
			for (int m = 0; m < list.size(); m++) {
				value = new LinkedList<String>();
				ExtAttendanceMachine extAttendanceMachine = list.get(m);
				value.add(extAttendanceMachine.getName());
				value.add(extAttendanceMachine.getDeviceCode());
				value.add(extAttendanceMachine.getFactoryCode());
				value.add(extAttendanceMachine.getSchoolName());
				if (extAttendanceMachine.getStatus()) {
					value.add("启用");
				} else {
					value.add("停用");
				}
				value.add(DateUtil.formatDateToString(extAttendanceMachine.getCreateTime(), "yyyy-MM-dd"));
				if ("01".equals(extAttendanceMachine.getLastReportStatus())) {
					value.add("正常");
				} else if ("02".equals(extAttendanceMachine.getLastReportStatus())) {
					value.add("故障");
				} else  if ("03".equals(extAttendanceMachine.getLastReportStatus())){
					value.add("其他原因");
				}
				listValue.add(value);
			}
			HSSFWorkbook workbook = exportDeviceList("考勤机列表", listParm, listValue);
			HttpServletResponse response = this.getResponse();
			response.setHeader("Content-disposition", "attachment;filename=attendanceMachines.xls");
			workbook.write(this.getResponse().getOutputStream());
		} catch (Exception e) {
			logger("AttendanceMachineController reportAttendanceMachine:" + e.getMessage(), e);
			toRedirect(500);
		}
		return success();
	}
}
